package com.yjf.demo.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回结构
 */
@Data
public class ResponseResult<T> implements Serializable {

    private Integer code;
    private String msg;
    private Long timestamp;
    private T data;
    public static final Integer SUCCESS_CODE = 200;
    public static final String SUCCESS_MESSAGE = "成功";

    public static final Integer fail_CODE = 701;
    public static final String fail_MESSAGE = "失败";


    public static final Integer Login_fail_CODE = 601;
    public static final String Login_fail_MESSAGE = "您的账号尚未登录，请重新登录！";

    public static final Integer Function_fail_CODE = 602;
    public static final String Function_fail_MESSAGE = "您的账号没有该权限！";


    public static final Integer LOGIN_FAIL_CODE = 601;

    public static final Integer HTTP_FAIL_CODE = 602;

    public static final Integer AUTH_FAIL_CODE = 603;

    public static final Integer VIDEO_FAIL_CODE = 1001;


    public ResponseResult() {
        this.timestamp = System.currentTimeMillis();
    }

    public ResponseResult(Integer code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }


    static public <S> ResponseResult<S> build(Integer code, String message) {
        return new ResponseResult<>(code, message, null);
    }


    static public <S> ResponseResult<S> success(S data) {
        return new ResponseResult<>(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    static public <S> ResponseResult<S> error(S data) {
        return new ResponseResult<>(fail_CODE, fail_MESSAGE, data);
    }

    static public <S> ResponseResult<S> errorMsg(String msg) {
        return new ResponseResult<>(fail_CODE, msg, null);
    }

    static public <S> ResponseResult<S> errorMsg(Integer code, String msg) {
        if (code == null) {
            code = fail_CODE;
        }
        return new ResponseResult<>(code, msg, null);
    }

    static public <S> ResponseResult<S> errorMsg(String msg, S data) {
        return new ResponseResult<>(fail_CODE, msg, data);
    }

    static public <S> ResponseResult<S> loginErrorMsg(int code) {
        if (code == Login_fail_CODE) {
            return new ResponseResult<>(code, Login_fail_MESSAGE, null);
        } else if (code == Function_fail_CODE) {
            return new ResponseResult<>(code, Function_fail_MESSAGE, null);
        }
        return new ResponseResult<>(code, Function_fail_MESSAGE, null);

    }

    static public <S> ResponseResult<S> loginErrorMsg(String msg) {
        return new ResponseResult<>(LOGIN_FAIL_CODE, msg, null);
    }

    static public <S> ResponseResult<S> authErrorMsg(String msg) {
        return new ResponseResult<>(AUTH_FAIL_CODE, msg, null);
    }
}